/*
 * webdav.js, v1.0, 2007-12-20
 */

/**
 * supported rdf file extensions
 */ 
var rdfExt = [
    ["n3", "n3", "N3 RDF", "text/rdf+n3"],
    ["rdfs", "rdfs", "RDFS/XML", "text/xml"],
    ["rdf", "rdf", "RDF/XML", "text/xml"],
    ["xml", "xml", "RDF/XML", "text/xml"]
]

/*
 * supported mapping file extensions
 */
var saveExt = [
    ["sql", "sql", "MetaSchema File", "text/plain"],
    ["semreg","semreg", "Simple Mapping File", "text/xml"]
]
function remote_f(){
    dialog_onto.hide();
    dialog_remote.show();
}
function remote_f2(thepath){
    dialog_remote.hide();
    var options = {
        extensionFilters:rdfExt,
        path:thepath,
        isDav:false};
    OAT.WebDav.init(options);
    function showContents(path, name, data) {
            //alert(path+name+' contains:\n\n'+data);
            var callback = function() {
                globalVar.ontos = [];
                interval.total = 0;
                statistic.ontology = 0;
                statistic.property = 0;
                var datas = store.data.structured;
                for (var i = 0; i < datas.length; i++) {
                    var uri = datas[i].uri;
                    var type = store.simplify(datas[i].type);
                    var preds = datas[i].preds;
                    var back = datas[i].back;
                    switch(type) {
                    case "Property":
                        // do nothing.
                        break;
                    case "Class":
                        var onto = new Ontology(uri, store.simplify(uri));
                        var ontoNode = globalVar.ontoView.insertOnto(onto);
                        ontoNode.collapse();
                        statistic.ontology++;
                        if (back) {
                            for (var j = 0; j < back.length; j++) {
                                var flags = false;
                                var domains = back[j].preds["http://www.w3.org/2000/01/rdf-schema#domain"];
                                for (var k = 0; k < domains.length; k++) {
                                    if (domains[k].uri == uri) {
                                        // not a properity of onto
                                        flags = true;
                                        break;
                                    }
                                }
                                if (!flags) {
                                    continue;
                                }
                                var ranges = back[j].preds["http://www.w3.org/2000/01/rdf-schema#range"];
                                for (var k = 0; k < ranges.length; k++) {
                                    var prop = new Property(back[j].uri, store.simplify(back[j].uri));
                                    if (typeof(ranges[k]) != 'string') {
                                        prop.isValue = false;
                                        prop.object = ranges[k].uri;
                                    } else {
                                        setTimeout(bind(globalVar.ontoView.insertProperty, [ontoNode, prop]), interval.total);
                                        interval.total += interval.loop;
                                        statistic.property++;
                                    }
                                    onto.addProperty(prop);
                                }
                            }
                        }
                        globalVar.ontos.push(onto);
                        break;
                    default:
                        // skip unknown type.
                        //alert("type: " + type + " is not handled, if it's a bug, contact us please.");
                    }
                }
            }
            var store = new OAT.RDFStore(callback, {});
            store.addURL(path + name, false, false);
        }
    OAT.WebDav.openDialog({callback:showContents});
}

function default_f(){
    dialog_onto.hide();
    var options = {
        extensionFilters:rdfExt,
        path:saveOption.rdfsPath,
        isDav:true,
        callback:function(path, name, data) {
            var callback = function() {
                globalVar.ontos = [];
                interval.total = 0;
                statistic.ontology = 0;
                statistic.property = 0;
                var datas = store.data.structured;
                for (var i = 0; i < datas.length; i++) {
                    var uri = datas[i].uri;
                    var type = store.simplify(datas[i].type);
                    var preds = datas[i].preds;
                    var back = datas[i].back;
                    switch(type) {
                    case "Property":
                        // do nothing.
                        break;
                    case "Class":
                        var onto = new Ontology(uri, store.simplify(uri));
                        var ontoNode = globalVar.ontoView.insertOnto(onto);
                        ontoNode.collapse();
                        statistic.ontology++;
                        if (back) {
                            for (var j = 0; j < back.length; j++) {
                                var flags = false;
                                var domains = back[j].preds["http://www.w3.org/2000/01/rdf-schema#domain"];
                                for (var k = 0; k < domains.length; k++) {
                                    if (domains[k].uri == uri) {
                                        // not a properity of onto
                                        flags = true;
                                        break;
                                    }
                                }
                                if (!flags) {
                                    continue;
                                }
                                var ranges = back[j].preds["http://www.w3.org/2000/01/rdf-schema#range"];
                                for (var k = 0; k < ranges.length; k++) {
                                    var prop = new Property(back[j].uri, store.simplify(back[j].uri));
                                    if (typeof(ranges[k]) != 'string') {
                                        prop.isValue = false;
                                        prop.object = ranges[k].uri;
                                    } else {
                                        setTimeout(bind(globalVar.ontoView.insertProperty, [ontoNode, prop]), interval.total);
                                        interval.total += interval.loop;
                                        statistic.property++;
                                    }
                                    onto.addProperty(prop);
                                }
                            }
                        }
                        globalVar.ontos.push(onto);
                        break;
                    default:
                        // skip unknown type.
                        //alert("type: " + type + " is not handled, if it's a bug, contact us please.");
                    }
                }
            }
            var store = new OAT.RDFStore(callback, {});
            store.addURL(path + name, false, false);
        }
    }
    OAT.WebDav.openDialog(options);
}


function local_f(){
dialog_onto.hide();
dialog_local.show();}

function local_f2(){
dialog_local.hide();
var options = {
        extensionFilters:rdfExt,
        path:"/map/upload.rdf",
        isDav:false};
OAT.WebDav.init(options);
    function showContents(path, name, data) {
            //alert(path+name+' contains:\n\n'+data);
            var callback = function() {
                globalVar.ontos = [];
                interval.total = 0;
                statistic.ontology = 0;
                statistic.property = 0;
                var datas = store.data.structured;
                for (var i = 0; i < datas.length; i++) {
                    var uri = datas[i].uri;
                    var type = store.simplify(datas[i].type);
                    var preds = datas[i].preds;
                    var back = datas[i].back;
                    switch(type) {
                    case "Property":
                        // do nothing.
                        break;
                    case "Class":
                        var onto = new Ontology(uri, store.simplify(uri));
                        var ontoNode = globalVar.ontoView.insertOnto(onto);
                        ontoNode.collapse();
                        statistic.ontology++;
                        if (back) {
                            for (var j = 0; j < back.length; j++) {
                                var flags = false;
                                var domains = back[j].preds["http://www.w3.org/2000/01/rdf-schema#domain"];
                                for (var k = 0; k < domains.length; k++) {
                                    if (domains[k].uri == uri) {
                                        // not a properity of onto
                                        flags = true;
                                        break;
                                    }
                                }
                                if (!flags) {
                                    continue;
                                }
                                var ranges = back[j].preds["http://www.w3.org/2000/01/rdf-schema#range"];
                                for (var k = 0; k < ranges.length; k++) {
                                    var prop = new Property(back[j].uri, store.simplify(back[j].uri));
                                    if (typeof(ranges[k]) != 'string') {
                                        prop.isValue = false;
                                        prop.object = ranges[k].uri;
                                    } else {
                                        setTimeout(bind(globalVar.ontoView.insertProperty, [ontoNode, prop]), interval.total);
                                        interval.total += interval.loop;
                                        statistic.property++;
                                    }
                                    onto.addProperty(prop);
                                }
                            }
                        }
                        globalVar.ontos.push(onto);
                        break;
                    default:
                        // skip unknown type.
                        //alert("type: " + type + " is not handled, if it's a bug, contact us please.");
                    }
                }
            }
            var store = new OAT.RDFStore(callback, {});
            store.addURL(path + name, false, false);
        }
    OAT.WebDav.openDialog({callback:showContents});
}

function loadRDF() {
    dialog_onto.show();
}


function saveMapping() {
    var options = {
        extensionFilters:saveExt,
        confirmOverwrite:true,
        path:saveOption.mappingPath,
        dataCallback:function(file, ext) {
            if (ext == "semreg") {
                var xml = buildMappingInfoDoc();
                return xml;
            } else if (ext == "sql") {
                var doc = mslInfo();
                return doc;
            }
        },
        callback:function(path, name, data) {
            var ch = confirm("Open saved meta schema language script file?");
            if (ch == true) {
                var url = escape(path + "/" + name);
                window.open(url);
            }
        }
    }
    OAT.WebDav.saveDialog(options);
}

function saveMappingFileToDisk() {
    if (!globalVar.projectId)
        return;
    var options = {
        extensionFilters:saveExt,
        path:saveOption.mappingPath,
        callback:function(path, name, data) {
            var url = escape(path + name);
            window.open(url);
        }
    };
    OAT.WebDav.openDialog(options);
}

/**
 * open meta schema script file.
 */
function openMappingFile() {
    var options = {
        extensionFilters:saveExt,
        path:saveOption.mappingPath,
        callback:function(path, name, data) {
            parseMetaSchema(data, showAllMappingUnits);
        }
    }
    OAT.WebDav.openDialog(options);
}
